<?php

namespace app\admin\model\customer;

use app\admin\model\Admin;
use think\Model;

class Intention extends Model
{
    // 表名
    protected $name = 'customer';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'admin_info',
        'total_amount',
        'audit_status'
    ];

    public function getAuditStatusAttr($value, $data)
    {
        $wtj = (new Package())->where([
            'customer_id' => $data['id'],
            'status' => 0,
        ])->count();

        $shz = (new Package())->where([
            'customer_id' => $data['id'],
            'status' => 1,
        ])->count();

        $shtg = (new Package())->where([
            'customer_id' => $data['id'],
            'status' => 2,
        ])->count();

        $shbtg = (new Package())->where([
            'customer_id' => $data['id'],
            'status' => 3,
        ])->count();

        return [
            'wtj' => $wtj,
            'shz' => $shz,
            'shtg' => $shtg,
            'shbtg' => $shbtg,
        ];
    }

    public function getAdminInfoAttr($value, $data)
    {
        return Admin::get($data['admin_id']);
    }


    public function getServiceIdsAttr($value, $data)
    {
        return $value ? array_map('intval', explode(',', $value)) : [];
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getTotalAmountAttr($value, $data)
    {
        return (new Service())->where(['customer_id' => $data['id']])->sum('deal_price');
    }

//    public function getStartTimeTextAttr($value, $data)
//    {
//        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
//        return is_numeric($value) ? date("Y年m月d日", $value) : $value;
//    }
//
//    public function getEndTimeTextAttr($value, $data)
//    {
//        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
//        return is_numeric($value) ? date("Y年m月d日", $value) : $value;
//    }

//    public function getCustomerServiceAttr($value, $data)
//    {
//        return (new Service)->where(['customer_id' => $data['id']])->select();
//    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    //保存合同
//    public function saveCustomerContract($customer_id)
//    {
//        $customer = $this->where(['id' => $customer_id])->find();
//        if (!$customer) {
//            return false;
//        }
//        $service = [];
//        $service_list = (new \app\admin\model\customer\Service)->where(['customer_id' => $customer_id])->select();
//        foreach ($service_list as $item) {
//            $service[] = '☑' . $item->service_name;
//        }
//
//
//        $standard_price = (new \app\admin\model\customer\Service)->where(['customer_id' => $customer_id])->sum('standard_price');
//        $ledger_expenses = (new \app\admin\model\customer\Service)->where(['customer_id' => $customer_id])->sum('ledger_expenses');
//
//        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template/ht.docx');
//
//        $admin_info = Admin::get($customer->admin_id);
//
////        $templateProcessor->setValue('name', $customer->customer_name ? $customer->customer_name : $customer->corporate_name);
////        $templateProcessor->setValue('corporate_name', $customer->corporate_name);
//        $templateProcessor->setValue('mobile', $customer->mobile);
//        $templateProcessor->setValue('admin_name', $admin_info->nickname);
//        $templateProcessor->setValue('admin_mobile', $admin_info->mobile);
//        $templateProcessor->setValue('service', implode('、', $service));
//        $templateProcessor->setValue('service_desc', $customer->service_desc);
//        $templateProcessor->setValue('start_time', $customer->start_time_text);
//        $templateProcessor->setValue('end_time', $customer->end_time_text);
//        $templateProcessor->setValue('standard_price', $standard_price);
//        $templateProcessor->setValue('ledger_expenses', $ledger_expenses);
//        $templateProcessor->setValue('pay_type1', $customer->pay_type == 1 ? '☑' : '☐');
//        $templateProcessor->setValue('pay_type2', $customer->pay_type == 2 ? '☑' : '☐');
//        if ($customer->signature_url) {
//            $templateProcessor->setImageValue('signature', ['path' => str_replace('/uploads', 'uploads', $customer->signature_url), 'width' => 100, 'height' => 40, 'ratio' => true]);
//        } else {
//            $templateProcessor->setValue('signature', '');
//        }
//        $contract_url = 'contract/' . $customer_id . time() . '.docx';
//        $customer->save(['contract_url' => $contract_url]);
//        $templateProcessor->saveAs($contract_url);
//    }
}
